package com.yunchang.springboot.scheduled.amazon.mws.report.datadispose;

import com.yunchang.springboot.mapper.mwsreport.IMwsMonthlyInventoryHistoryMapper;
import com.yunchang.springboot.models.Shop;
import com.yunchang.springboot.models.mwsreportbean.MwsMonthlyInventoryHistory;
import com.yunchang.springboot.models.mwsreportbean.MwsMonthlyInventoryHistoryDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.parent.ReportDataDisposeParent;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.reportinterface.IReportDataDispose;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.StringUtil;
import com.yunchang.springboot.utils.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author czw
 * @date 2020/7/28 0028  18:49
 * @description:
 */
@Component
public class EveryMonthInventoryRecordsDataDispose extends ReportDataDisposeParent implements IReportDataDispose {

    @Autowired
    IMwsMonthlyInventoryHistoryMapper mwsMonthlyInventoryHistoryMapper;
    /**
     * 每月库存记录  将获取到的数据转为实体对象数组
     *
     * @param lists
     * @param shopBrandName
     * @param area
     * @return
     * @throws Exception
     */
    @Override
    public List<Object> reportDataDispose(List<List<String>> lists, String shopBrandName, String area,String sellerId) throws Exception {
        List<Object> insertList = new ArrayList<>(lists.size());
        List<String> titleList = lists.get(0);
        Map<String, Object> titleMap = new HashMap<>(titleList.size());
        for (int i = 0; i < titleList.size(); i++) {
            titleMap.put(titleList.get(i), i);
        }
        // 不用区域的表头有所不同，目前是日本区域的不同需要做额外处理
        if (null != area && area.equals(ESPECIALLY_AREA) && !shopBrandName.equals(SPECIAL_SHOP_NAME_TWO) && !shopBrandName.equals(SPECIAL_SHOP_NAME)) {
            int monthIndex = Integer.valueOf(String.valueOf(titleMap.get("Month")));
            String latestDay = mwsMonthlyInventoryHistoryMapper.getLatestDay(sellerId, area);
            if (!StringUtil.isBlank(latestDay)) {
                List<String> firstList = lists.get(1);
                String m = firstList.get(monthIndex);
                if (DateUtil.compateTime(latestDay, (m.substring(3, 7) + "-" + m.substring(0, 2))) >= 0) {
                    return null;
                }
            }
            int fnskuIndex = Integer.valueOf(String.valueOf(titleMap.get("FNSKU")));
            int skuIndex = Integer.valueOf(String.valueOf(titleMap.get("Merchant SKU")));
            int productNameIndex = Integer.valueOf(String.valueOf(titleMap.get("Title")));
            int averageQuantityIndex = Integer.valueOf(String.valueOf(titleMap.get("Avg Qty")));
            int endQuantityIndex = Integer.valueOf(String.valueOf(titleMap.get("End Qty")));
            int fulfillmentCenterIdIndex = Integer.valueOf(String.valueOf(titleMap.get("FC")));
            int detailedDispositionIndex = Integer.valueOf(String.valueOf(titleMap.get("Disposition")));
            int countryIndex = Integer.valueOf(String.valueOf(titleMap.get("Country")));
            for (int i = 1; i < lists.size(); i++) {
                MwsMonthlyInventoryHistoryDO records = new MwsMonthlyInventoryHistoryDO();
                List<String> row = lists.get(i);
                records.setSellerId(sellerId);
                records.setArea(area);
                String month = row.get(monthIndex);
                records.setMonth(month.substring(3, 7) + "-" + month.substring(0, 2));
                records.setFnsku(row.get(fnskuIndex));
                records.setSku(row.get(skuIndex));
                records.setProductName(row.get(productNameIndex));
                records.setAverageQuantity(new BigDecimal(String.valueOf(row.get(averageQuantityIndex))));
                records.setEndQuantity(new BigDecimal(String.valueOf(row.get(endQuantityIndex))));
                records.setFulfillmentCenterId(row.get(fulfillmentCenterIdIndex));
                records.setDetailedDisposition(row.get(detailedDispositionIndex));
                records.setCountry(row.get(countryIndex));
                records.setCreateTime(DateUtil.getDate());
                records.setUpdateTime(DateUtil.getDate());
                records.setFinanceStatus(1);
                insertList.add(records);
            }
            return insertList;
        } else {
            int monthIndex = Integer.valueOf(String.valueOf(titleMap.get("month")));
            String latestDay = mwsMonthlyInventoryHistoryMapper.getLatestDay(sellerId, area);
            if (!StringUtil.isBlank(latestDay)) {
                List<String> firstList = lists.get(1);
                String m = firstList.get(monthIndex);
                if (DateUtil.compateTime(latestDay, (m.substring(3, 7) + "-" + m.substring(0, 2))) >= 0) {
                    return null;
                }
            }
            int fnskuIndex = Integer.valueOf(String.valueOf(titleMap.get("fnsku")));
            int skuIndex = Integer.valueOf(String.valueOf(titleMap.get("sku")));
            int productNameIndex = Integer.valueOf(String.valueOf(titleMap.get("product-name")));
            int averageQuantityIndex = Integer.valueOf(String.valueOf(titleMap.get("average-quantity")));
            int endQuantityIndex = Integer.valueOf(String.valueOf(titleMap.get("end-quantity")));
            int fulfillmentCenterIdIndex = Integer.valueOf(String.valueOf(titleMap.get("fulfillment-center-id")));
            int detailedDispositionIndex = Integer.valueOf(String.valueOf(titleMap.get("detailed-disposition")));
            int countryIndex = Integer.valueOf(String.valueOf(titleMap.get("country")));
            for (int i = 1; i < lists.size(); i++) {
                MwsMonthlyInventoryHistoryDO records = new MwsMonthlyInventoryHistoryDO();
                List<String> row = lists.get(i);
                records.setSellerId(sellerId);
                records.setArea(area);
                String month = row.get(monthIndex);
                records.setMonth(month.substring(3, 7) + "-" + month.substring(0, 2));
                records.setFnsku(row.get(fnskuIndex));
                records.setSku(row.get(skuIndex));
                records.setProductName(row.get(productNameIndex));
                records.setAverageQuantity(new BigDecimal(String.valueOf(row.get(averageQuantityIndex))));
                records.setEndQuantity(new BigDecimal(String.valueOf(row.get(endQuantityIndex))));
                records.setFulfillmentCenterId(row.get(fulfillmentCenterIdIndex));
                records.setDetailedDisposition(row.get(detailedDispositionIndex));
                records.setCountry(row.get(countryIndex));
                records.setCreateTime(DateUtil.getDate());
                records.setUpdateTime(DateUtil.getDate());
                records.setFinanceStatus(1);
                insertList.add(records);
            }
            return insertList;
        }
    }
}
